## Hadamard matrix of a finite field with $q+1\equiv 0 \mod4$

from PyM import *

def paley_matrix(F):
    if characteristic(F)==2:
        return "paley_matrix error: the characteristic of F must not be 2"
    q = cardinal(F)
    def x(j): return element(j,F)
    def chi(a): return legendre(a,F)
    return matrix([[chi(x(i)-x(j)) for j in range(q)] for i in range(q)])


def hadamard_matrix_paley(K):
    q = cardinal(K)
    if q%4 != 3:
        return "hadamard_matrix_FF Error: {} is not 3 mod 4".format(q)
    A = I_(q,Z_) + paley_matrix(K)
    u = matrix(q*[1])
    x = splice(matrix([1]),u)
    X = splice(transpose(u),-A)
    return stack(x,X)
    
show(hadamard_matrix_paley(Zn(7)))